<?php
namespace app\common\logic;
use think\Db;
use think\Exception;

class QxcPayment
{
    private static $_instance;
    
    private function __construct() {}
    
    public static function getInstance()
    {
        if(!(self::$_instance instanceof self)) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    public function dealInfo($data) {
        $kjjg_arr = str_split(trim($data['kjjg']),1);
        $code_arr = explode(',',$data['tzhm']);
        $kjjg_1 = $kjjg_arr[0];
        $kjjg_2 = $kjjg_arr[1];
        $kjjg_3 = $kjjg_arr[2];
        $kjjg_4 = $kjjg_arr[3];
        $kjjg_5 = $kjjg_arr[4];
        $kjjg_6 = $kjjg_arr[5];
        $kjjg_7 = $kjjg_arr[6];
        $zjzs = 0;
        $zjje = 0;
    try{
        switch( intval($data['wfid']) ) {
        	case 264:
                //七星直选
        	    if(count($code_arr)==7){
        	        if(strpos($code_arr[0],$kjjg_1) !== false){
        	            if(strpos($code_arr[1],$kjjg_2) !== false){
        	                if(strpos($code_arr[2],$kjjg_3) !== false){
        	                    if(strpos($code_arr[3],$kjjg_4) !== false){
        	                        if(strpos($code_arr[4],$kjjg_5) !== false){
        	                            if(strpos($code_arr[5],$kjjg_6) !== false){
        	                                if(strpos($code_arr[6],$kjjg_7) !== false){
        	                                    $zjzs=$zjzs+1;
        	                                }
        	                            }
        	                        }
        	                    }
        	                }
        	            }
        	        }
        	    }
        	    break;
        	case 265:
                //前六直选
                if(count($code_arr)==6){
                    if(strpos($code_arr[0],$kjjg_1) !== false){
                        if(strpos($code_arr[1],$kjjg_2) !== false){
                            if(strpos($code_arr[2],$kjjg_3) !== false){
                                if(strpos($code_arr[3],$kjjg_4) !== false){
                                    if(strpos($code_arr[4],$kjjg_5) !== false){
                                        if(strpos($code_arr[5],$kjjg_6) !== false){
                                            $zjzs=$zjzs+1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            case 266:
                //后六直选
                if(count($code_arr)==6){
                    if(strpos($code_arr[0],$kjjg_2) !== false){
                        if(strpos($code_arr[1],$kjjg_3) !== false){
                            if(strpos($code_arr[2],$kjjg_4) !== false){
                                if(strpos($code_arr[3],$kjjg_5) !== false){
                                    if(strpos($code_arr[4],$kjjg_6) !== false){
                                        if(strpos($code_arr[5],$kjjg_7) !== false){
                                            $zjzs=$zjzs+1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            case 267:
                //前五直选
                if(count($code_arr)==5){
                    if(strpos($code_arr[0],$kjjg_1) !== false){
                        if(strpos($code_arr[1],$kjjg_2) !== false){
                            if(strpos($code_arr[2],$kjjg_3) !== false){
                                if(strpos($code_arr[3],$kjjg_4) !== false){
                                    if(strpos($code_arr[4],$kjjg_5) !== false){
                                        $zjzs=$zjzs+1;
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            case 268:
                //后五直选
                if(count($code_arr)==5){
                    if(strpos($code_arr[0],$kjjg_3) !== false){
                        if(strpos($code_arr[1],$kjjg_4) !== false){
                            if(strpos($code_arr[2],$kjjg_5) !== false){
                                if(strpos($code_arr[3],$kjjg_6) !== false){
                                    if(strpos($code_arr[4],$kjjg_7) !== false){
                                        $zjzs=$zjzs+1;
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            case 269:
                //前四直选
                if(count($code_arr)==4){
                    if(strpos($code_arr[0],$kjjg_1) !== false){
                        if(strpos($code_arr[1],$kjjg_2) !== false){
                            if(strpos($code_arr[2],$kjjg_3) !== false){
                                if(strpos($code_arr[3],$kjjg_4) !== false){
                                    $zjzs=$zjzs+1;
                                }
                            }
                        }
                    }
                }
                break;
            case 270:
                //后四直选
                if(count($code_arr)==4){
                    if(strpos($code_arr[0],$kjjg_4) !== false){
                        if(strpos($code_arr[1],$kjjg_5) !== false){
                            if(strpos($code_arr[2],$kjjg_6) !== false){
                                if(strpos($code_arr[3],$kjjg_7) !== false){
                                    $zjzs=$zjzs+1;
                                }
                            }
                        }
                    }
                }
                break;
            case 271:
                //前三直选
                if(count($code_arr)==3){
                    if(strpos($code_arr[0],$kjjg_1) !== false){
                        if(strpos($code_arr[1],$kjjg_2) !== false){
                            if(strpos($code_arr[2],$kjjg_3) !== false){
                                $zjzs=$zjzs+1;
                            }
                        }
                    }
                }
                break;
            case 272:
                //中三直选
                if(count($code_arr)==3){
                    if(strpos($code_arr[0],$kjjg_3) !== false){
                        if(strpos($code_arr[1],$kjjg_4) !== false){
                            if(strpos($code_arr[2],$kjjg_5) !== false){
                                $zjzs=$zjzs+1;
                            }
                        }
                    }
                }
                break;
            case 273:
                //后三直选
                if(count($code_arr)==3){
                    if(strpos($code_arr[0],$kjjg_5) !== false){
                        if(strpos($code_arr[1],$kjjg_6) !== false){
                            if(strpos($code_arr[2],$kjjg_7) !== false){
                                $zjzs=$zjzs+1;
                            }
                        }
                    }
                }
                break;
            case 274:
                //前二直选
                if(count($code_arr)==2){
                    if(strpos($code_arr[0],$kjjg_1) !== false){
                        if(strpos($code_arr[1],$kjjg_2) !== false){
                            $zjzs=$zjzs+1;
                        }
                    }
                }
                break;
            case 275:
                //后二直选
                if(count($code_arr)==2){
                    if(strpos($code_arr[0],$kjjg_6) !== false){
                        if(strpos($code_arr[1],$kjjg_7) !== false){
                            $zjzs=$zjzs+1;
                        }
                    }
                }
                break;
            case 276:
                //七星定位胆
                if(count($code_arr)==7){
                    if(strpos($code_arr[0],$kjjg_1) !== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($code_arr[1],$kjjg_2) !== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($code_arr[2],$kjjg_3) !== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($code_arr[3],$kjjg_4) !== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($code_arr[4],$kjjg_5) !== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($code_arr[5],$kjjg_6) !== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($code_arr[6],$kjjg_7) !== false){
                        $zjzs=$zjzs+1;
                    }
                }
                break;
        	    
        }
    }catch (Exception $e){
        echo $e->getMessage() ;
        return false;
    }
        //结算中奖并转移结算数据
        if($data['nb']==1 || $data['swzh']==1) {
            $table = 'touzhu_list_sw';
        } else {
            $table = 'touzhu_list_ok';
        }
        
        //选择投注模式
        if($data['nbzh'] == 1) $pieces = 1;
        if($data['nbzh'] == 2) $pieces = 10;
        if($data['nbzh'] == 3) $pieces = 100;
        
        //处理中奖金额（赔率*单注金额*中奖注数/投注模式），特殊玩法除外
        $zjje = $data['dqpl'] * $data['dzje'] * $zjzs / $pieces;
        
        //转移投注注单
        $insert_data = $data;
        $is_zh = $insert_data['from'];
        unset($insert_data['user_id'],$insert_data['swzh'],$insert_data['nb'],$insert_data['rktime'],$insert_data['cp_name_old'],$insert_data['cp_name_new'],$insert_data['from']);
        $insert_data['ifqs'] = '1';
        $insert_data['zjje'] = $zjje;
        $insert_data['zjzs'] = $zjzs;
        
        $insert_pc = $insert_data;
        unset($insert_pc['tzhje'],$insert_pc['tz_time']);
        $tzhje = Db::name('username')->where('id',$data['user_id'])->value('je');
        $insert_pc['tzhje'] = $tzhje;
        $insert_pc['tz_time'] = time();
        
        Db::startTrans();
        try {
        	if(!Db::name($table)->data($insert_data)->insert()){
        		throw new \Exception(false);
        	}
        	
        	if(!Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete()){
        		throw new \Exception(false);
        	}
        	
        	//中奖金额大于0.001则给用户增加金额
        	if($zjje > 0.001) {
        		if(!Db::name('touzhu_list_pc')->data($insert_pc)->insert()){
        			throw new \Exception(false);
        		}
        		//$prefix = config('database.prefix');
        		//Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$data['user_id']}");
        		if(!Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]])){
        			throw new \Exception(false);
        		}
                //处理追号自动停止
                if( 1==intval($is_zh) ){
                    $list_zh = Db::name('touzhu_list_zh')->field('*,1 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'stop'=>1])->select();
                    $list_no = Db::name('touzhu_list_no')->field('*,2 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'from'=>1])->select();
                    $list = array_merge($list_zh,$list_no);
                    $tkqje = Db::name('username')->where('id',$data['user_id'])->value('je');
                    //$tkqje = floatval($tzhje) + floatval($zjje);
                    foreach($list as $v){
                        $id = $v['id'];
                        unset($v['id']);
                        if( 1==$v['type'] ){
                            unset($v['type']);
                            unset($v['stop']);
                            if( !Db::name('touzhu_list_zh')->delete($id) ){
                                throw new \Exception(false);
                            }
                        }else{
                            unset($v['rktime']);
                            unset($v['type']);
                            unset($v['from']);
                            if( !Db::name('touzhu_list_no')->delete($id) ){
                                throw new \Exception(false);
                            }
                        }
                        $v['tktime'] = time();
                        $v['tkqje'] = $tkqje;
                        $tkqje += floatval($v['tzje']);

                        if( !Db::name('touzhu_list_tk')->insert($v) ){
                            throw new \Exception(false);
                        }
                        if( !Db::name('username')->where('id',$data['user_id'])->update(array('je'=>['exp',"je+'{$v['tzje']}'"],'tzzje'=>['exp',"tzzje-'{$v['tzje']}'"])) ){
                            throw new \Exception(false);
                        }
                    }
                }
        	}
            /* Db::name($table)->data($insert_data)->insert();
            Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete();
            //中奖金额大于0.001则给用户增加金额
            if($zjje > 0.001) {
                //$prefix = config('database.prefix');
                //Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$data['user_id']}");
                Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]]);
            } */
            Db::commit();
            return $zjje;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
        
    }
}